The growth of choppies across Botswana¶

In [111]:
choppies <- read.csv("choppies.csv")
In [112]:
choppies$Date <- as.Date(choppies$Date, "%d/%m/%Y")
choppies$year <- as.numeric(format(choppies$Date, "%Y"))
choppies_map$District <- factor(choppies_map$District)
In [94]:
str(choppies)
'data.frame':	108 obs. of  8 variables:
 $ Name     : chr  "Choppies Bobonong" "Choppies Letlhakane" "Choppies Mahalapye" "Choppies Mahalapye" ...
 $ District : chr  "Central" "Central" "Central" "Central" ...
 $ Town     : chr  "Bobonong" "Letlhakane" "Mahalapye" "Mahalapye" ...
 $ Address  : chr  "Moilamba Ward,Cash Bazaar building" "Nkosho Ward,Letlhakane" "Plot 6042, Main Mall, Mahalapye" "Watershed Mall, A1,  Mahalapye" ...
 $ latitude : num  -22 -21.4 -23.1 -23.1 NA ...
 $ longitude: num  28.4 25.6 26.8 26.8 NA ...
 $ Date     : chr  "12/09/2012" "12/10/2007" "13/01/2012" "" ...
 $ Registred: chr  "Y" "Y" "Y" "Y" ...
In [95]:
summary(choppies)
     Name             District             Town             Address         
 Length:108         Length:108         Length:108         Length:108        
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
                                                                            
    latitude        longitude         Date            Registred        
 Min.   :-26.02   Min.   :21.64   Length:108         Length:108        
 1st Qu.:-24.66   1st Qu.:25.55   Class :character   Class :character  
 Median :-24.61   Median :25.87   Mode  :character   Mode  :character  
 Mean   :-23.48   Mean   :25.81                                        
 3rd Qu.:-21.98   3rd Qu.:26.11                                        
 Max.   :-17.80   Max.   :28.42                                        
 NA's   :29       NA's   :29                                           
In [5]:
library(dplyr)
Attaching package: 'dplyr'


The following objects are masked from 'package:stats':

    filter, lag


The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union


In [ ]:
Removing NAs...
In [124]:
choppies_map <- choppies %>% 
filter(!is.na(longitude),!is.na(latitude), !is.na(Date)) %>%
data.frame
In [125]:
str(choppies_map)
'data.frame':	74 obs. of  9 variables:
 $ Name     : chr  "Choppies Bobonong" "Choppies Letlhakane" "Choppies Mahalapye" "Choppies Palapye" ...
 $ District : chr  "Central" "Central" "Central" "Central" ...
 $ Town     : chr  "Bobonong" "Letlhakane" "Mahalapye" "Palapye" ...
 $ Address  : chr  "Moilamba Ward,Cash Bazaar building" "Nkosho Ward,Letlhakane" "Plot 6042, Main Mall, Mahalapye" "Makwapa complex,Serorome Ward,Palapye" ...
 $ latitude : num  -22 -21.4 -23.1 -22.5 -22.5 ...
 $ longitude: num  28.4 25.6 26.8 27.1 27.1 ...
 $ Date     : Date, format: "2012-09-12" "2007-10-12" ...
 $ Registred: chr  "Y" "Y" "Y" "Y" ...
 $ year     : num  2012 2007 2012 2005 2020 ...
In [126]:
summary(choppies_map)
     Name             District             Town             Address         
 Length:74          Length:74          Length:74          Length:74         
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
    latitude        longitude          Date             Registred        
 Min.   :-26.02   Min.   :21.64   Min.   :1999-11-02   Length:74         
 1st Qu.:-24.66   1st Qu.:25.52   1st Qu.:2005-09-13   Class :character  
 Median :-24.61   Median :25.88   Median :2011-08-06   Mode  :character  
 Mean   :-23.42   Mean   :25.80   Mean   :2010-12-12                     
 3rd Qu.:-21.97   3rd Qu.:26.13   3rd Qu.:2013-11-11                     
 Max.   :-17.80   Max.   :28.42   Max.   :2021-10-28                     
      year     
 Min.   :1999  
 1st Qu.:2005  
 Median :2011  
 Mean   :2010  
 3rd Qu.:2013  
 Max.   :2021  
In [48]:
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
library(plotly)
library(viridis)
Loading required package: viridisLite

In [218]:
#install.packages("devtools") # I guess you also need this
#devtools::install_github("ropensci/rnaturalearthhires")
library("rnaturalearth")
In [219]:
BW_states <- ne_states(country = 'botswana', returnclass = 'sf')
#str(BW_states)
In [221]:
set.seed(3)
g <- ggplot(data = BW_states) + geom_sf() +
geom_jitter(data = choppies_map, 
           mapping=aes(x = longitude, y = latitude, 
               colour = District, alpha = 0.5), 
            width = 0.1, height = 0.15) +
scale_color_viridis_d()

ggplotly(g)
In [ ]:
set.seed(3)
g <- ggplot(data = BW_states) + geom_sf() +
geom_jitter(data = choppies_map, 
           mapping=aes(x = longitude, y = latitude, 
               colour = District, alpha = 0.5), 
            width = 0.1, height = 0.15) +
scale_color_viridis_d()

ggplotly(g)
In [145]:
library(gganimate)
library(gifski)
In [214]:
set.seed(3)
map_with_data <- ggplot(data = BW_states) + geom_sf() +
geom_jitter(data = choppies_map, 
           mapping=aes(x = longitude, y = latitude, 
               colour = District, alpha = 0.5, group = year), 
            width = 0.1, height = 0.15) +
scale_color_viridis_d()
In [215]:
num_years <- max(choppies_map$year) - min(choppies_map$year) + 1

map_with_animation <- map_with_data +
    transition_time(year) +
ggtitle('The growth of choppies: {frame_time}')

map_with_animation <- map_with_animation +
shadow_mark()


animate(map_with_animation, nframes = num_years, fps = 1)
anim_save('choppies_growth.gif')